<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *员工管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'采购订单'),
);
$return['statuss']=$this->jinorder_status;
$return['paytype']=get_pay_types();
$return['paytype']['peerpay']='找人代付';
$return['paytype']['offpay']='线下收款';
$return['paytype']['offsao']='线下扫码';
$return['ispay']=array('0'=>'未支付_No pay','1'=>'已支付_Is pay');
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['id'])){
		$params['id']=$_RQ['id'];
		$return['params']['id']=$_RQ['id'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_erp_jinorder',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['pcd']=json_decode($item['pcd'],true);
			$item['goods']=json_decode($item['goods'],true);
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'id','title'=>'订单ID','type'=>'string'),
			array('name'=>'needtime','title'=>'需求时间','type'=>'time'),
			array('name'=>'name','title'=>'收件姓名','type'=>'string'),
			array('name'=>'mobile','title'=>'收件电话','type'=>'string'),
			array('name'=>'pcd','title'=>'区域地址','type'=>'pcd'),
			array('name'=>'address','title'=>'详细地址','type'=>'string'),
			array('name'=>'price','title'=>'金额','type'=>'string'),
			array('name'=>'remark','title'=>'备注','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'statuss'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		)),
		'search'=>array(
			array('name'=>'id','width'=>10,'title'=>'订单号ID','type'=>'string'),
			array('name'=>'status','width'=>10,'title'=>'订单状态_Status','type'=>'radio','options'=>'statuss'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$data=array(
			'groupid'=>trim($post['groupid']),
			'group2id'=>trim($post['group2id']),
			'needtime'=>strtotime($post['needtime']),
			'mobile'=>trim($post['mobile']),
			'name'=>trim($post['name']),
			'price'=>trim($post['price']),
			'pcd'=>ijson_encode($post['pcd']),
			'goods'=>ijson_encode($post['goods']),
			'address'=>trim($post['address']),
			'remark'=>trim($post['remark']),
		);
		if(!$id){
			$data['uniacid']=$_SESSION['uniacid'];
			$data['createtime']=time();
			pdo_insert('jy_erp_jinorder',$data);
		}else{
			pdo_update('jy_erp_jinorder',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
		exi('操作成功！','','list');
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_erp_jinorder',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	if(!$id){
		$item=pdo_get('jy_erp_jinorder',array('uniacid'=>$_SESSION['uniacid']),'','id desc');
		unset($item['id']);
		unset($item['needtime']);
		unset($item['remark']);
	}
	$item['needtime'] =$item['needtime']?date('Y/m/d H:i',$item['needtime']):'';
	$item['pcd']=json_decode($item['pcd'],true);
	$item['goods']=json_decode($item['goods'],true);
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'采购申请单',
		'edit'=>array(
			array('title'=>'这里《采购申请单》会产生一条真实的订单，其他部门会审批，审批通过后，会有专人下单采购！','type'=>'tip'),
			array('type'=>'select2_line','name'=>'groupid','options'=>'groups','title'=>'选择部门',
				'name2'=>'group2id','options2'=>'groups2','title2'=>'选择职位',
			),
			array('name'=>'needtime','title'=>'需求时间','type'=>'time'),
			array('name'=>'price','title'=>'采购金额','type'=>'string'),
			array('name'=>'name','title'=>'收件姓名','type'=>'string'),
			array('name'=>'mobile','title'=>'收件电话','type'=>'string'),
			array('name'=>'pcd','title'=>'区域地址','type'=>'pcd'),
			array('name'=>'address','title'=>'详细地址','type'=>'string'),
			array('name'=>'remark','title'=>'备注','type'=>'text'),
			array('type'=>'array','name'=>'goods','title'=>'采购的商品','add_tip'=>'添加一个商品',
						'list'=>array(
							array('type'=>'select','name'=>'pcate','options'=>'allgroups','title'=>'一级分类'),
							array('type'=>'select','name'=>'ccate','options'=>'allgroups','title'=>'二级分类'),
							array('type'=>'select','name'=>'goodsid','options'=>'goods','title'=>'商品名称'),
							array('type'=>'string','name'=>'total','title'=>'数量'),
						),
						'details'=>array(
							array('type'=>'select3_line','name'=>'pcate','options'=>'pcategoods','title'=>'选择分类',
							'name2'=>'ccate','options2'=>'ccates','title2'=>'二级分类',
							'name3'=>'goodsid','options3'=>'goods','title3'=>'请选择商品',
							),
							array('type'=>'string','name'=>'total','title'=>'数量'),
						)
			),
		)
	);
	$user_juese=$this->user_juese;
	$return['groups']=array();
	foreach($user_juese as $key=>$title){
		$return['groups'][$key]=array('id'=>$key,'title'=>$title);
		$pv['groups2']=pdo_getall('jy_yxw_worker',array('uniacid'=>$_SESSION['uniacid'],'juese'=>$key),array('title','id'),'id');
	}
	$goodsspram=array('uniacid'=>$_SESSION['uniacid'],'merchid OR'=>'showgroups=\'0\' or pcate>\'0\'');
	$goodsspram['onepluginname']='';
	$return['goods']=pdo_getall('jy_weishop_goods',$goodsspram,array('id','title'),'id');
	$groupspram=array('uniacid'=>$_SESSION['uniacid'],'mid'=>0,'parentid'=>0);
	$groupspram['onepluginname']='';
	$return['pcategoods']=pdo_getall('jy_weishop_goods_group',$groupspram,array('title','id'),'id');
	foreach($return['pcategoods'] as &$gv){
			$gv['ccates']=pdo_getall('jy_weishop_goods_group',array('uniacid'=>$_SESSION['uniacid'],'parentid'=>$gv['id']),array('title','id'),'id');
			$gv['ccates'][0]=array('id'=>0,'title'=>'默认分类');
			$gv['ccates'][0]['goods']=pdo_getall('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'ccate'=>0,'pcate'=>$gv['id']),array('title','id'),'id');
			foreach($gv['ccates'] as &$gcv){
				$gcv['goods']=pdo_getall('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'ccate'=>$gcv['id'],'pcate'=>$gv['id']),array('title','id'),'id');
			}
	}
	$groupspram=array('uniacid'=>$_SESSION['uniacid'],'mid'=>0,'merchid'=>0);
	$return['allgroups']=pdo_getall('jy_weishop_goods_group',$groupspram,array('title','id'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_erp_viplog',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
